package com.tang.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.tang.auth.application.convert.AuthPermissionDTOConverter;
import com.tang.auth.entity.AuthPermissionDTO;
import com.tang.auth.domain.entity.AuthPermissionBO;
import com.tang.auth.domain.service.AuthPermissionDomainService;
import com.tang.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 增加权限
     *
     * @param authPermissionDTO 待添加的权限数据
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 14:32
     **/
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "权限key不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(), "权限type不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean flag = authPermissionDomainService.add(authPermissionBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.PermissionController.add.error:{}", JSON.toJSONString(authPermissionDTO), e);
            return Result.fail(false);
        }
    }

    /**
     * 删除权限
     *
     * @param authPermissionDTO 待删除的权限数据
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 14:32
     **/
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean flag = authPermissionDomainService.delete(authPermissionBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.PermissionController.delete.error:{}", JSON.toJSONString(authPermissionDTO), e);
            return Result.fail(false);
        }
    }

    /**
     * 更新权限
     *
     * @param authPermissionDTO 待删除的权限数据
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 14:32
     **/
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            Boolean flag = authPermissionDomainService.update(authPermissionBO);
            return Result.ok(flag);
        } catch (Exception e) {
            log.error("com.tang.application.controller.PermissionController.update.error:{}", JSON.toJSONString(authPermissionDTO), e);
            return Result.fail(false);
        }
    }

    /**
     * 更新权限
     *
     * @param userName 用户id
     * @return com.tang.common.entity.Result<java.lang.Boolean>
     * @author Tang
     * @date 2024/2/15 14:32
     **/
    @GetMapping("/getPermission")
    public Result<List<String>> update(@RequestParam String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("com.tang.application.controller.PermissionController.update.dto:{}", userName);
            }
            Preconditions.checkNotNull(userName, "用户名不能为空");
            List<String> authList = authPermissionDomainService.getPermissionList(userName);
            return Result.ok(authList);
        } catch (Exception e) {
            log.error("com.tang.application.controller.PermissionController.update.error:{}", userName, e);
            return Result.fail(false);
        }
    }
}
